package com.example.questionsystem.modules.qu.controller;


import com.example.questionsystem.modules.qu.service.AnswerService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api/answers")
public class AnswerController {

    private final AnswerService answerService;

    public AnswerController(AnswerService answerService) {
        this.answerService = answerService;
    }

    @PostMapping("/convert")
    public ResponseEntity<byte[]> convertWordToExcel(@RequestParam("file") MultipartFile file) throws IOException {
        if (file.isEmpty()) {
            return ResponseEntity.badRequest().body("请上传文件".getBytes());
        }

        if (!file.getOriginalFilename().endsWith(".docx")) {
            return ResponseEntity.badRequest().body("仅支持.docx格式文件".getBytes());
        }

        byte[] excelBytes = answerService.convertWordToExcel(file);
        
        return ResponseEntity.ok()
                .header("Content-Disposition", "attachment; filename=answers.xlsx")
                .header("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                .body(excelBytes);
    }
}